import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { registerMicroApps, start } from 'qiankun'

Vue.config.productionTip = false

function genActiveRule(routerPrefix) {
  return (location) => location.pathname.startsWith(routerPrefix)
}
// 添加路由
router.addRoute('workbench', {
  path: `/workbench/p2`,
  name: `workbench.p2`,
  component(resolve) {
    require(['./views/component'], resolve)
  },
})

router.addRoute('root', {
  path: `/p2`,
  name: `p2`,
  component(resolve) {
    require(['./views/component'], resolve)
  },
})

const apps = [
  // {
  //   name: 'p1',
  //   entry: process.env.NODE_ENV === 'development' ? `//${location.hostname}:20002` : ``,
  //   container: '#microapp',
  //   activeRule: '/p1',
  //   loader: (loadStatus) => {},
  // },
  {
    name: 'p2',
    entry: process.env.NODE_ENV === 'development' ? `//${location.hostname}:20003` : ``,
    container: '#microapp',
    activeRule: genActiveRule('/p2'),
    loader: (loadStatus) => {},
  },
  {
    name: 'workbench.p2',
    // entry: 'http://192.168.26.237:9001',
    entry: process.env.NODE_ENV === 'development' ? `//${location.hostname}:20003` : ``,
    container: '#microapp',
    activeRule: genActiveRule('/workbench/p2'),
    loader: (loadStatus) => {},
    props: {
      contentOnly: true,
    },
  },
]

registerMicroApps(apps, {
  // 挂载前回调
  beforeLoad: (app) => {
    console.log('加载前回调')
  },
  // 挂载后回调
  beforeMount: (app) => {
    console.log('挂载之前回调')
  },
  afterMount: (app) => {
    console.log('挂在完成回调')
  },
  // 卸载后回调
  afterUnmount: (app) => {
    console.log('卸载后回调')
  },
})
start()

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#main-app')
